#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author:Summerday

import csv
from matplotlib import pyplot as plt
import networkx as nx
from numpy import random as nprand

plt.rcParams.update({
	'figure.figsize': (7.5, 7.5),
	'axes.spines.right': False,
	'axes.spines.left': False,
	'axes.spines.top': False,
	'axes.spines.bottom': False})

	# Seed random number generator

	
seed = hash("Network Science in Python") % 2 ** 32
nprand.seed(seed)
G = nx.Graph()
with open("csv/nodes.csv", 'r', encoding='utf-8', newline='') as f:
	next(f)
	r = csv.reader(f)
	for row in r:
		#personId = row[0], indexYear = row[2], addrName = row[3], x = row[4], y = row[5]
		personId = row[0]#作为id
		G.add_node(personId)
		g = G.nodes[personId]
		g["female"] = '男' if row[1] == '1' else '女'
		g["index_year"] = row[2]
		g["addr_name"] = row[3]
		g["x"] = row[4]
		g["y"] = row[5]

with open("csv/edges.csv", 'r', encoding='utf-8', newline='') as f:
	next(f)
	r = csv.reader(f)
	for row in r:
		#sourceId,sourceName,assocType,targetId,assocDesc
		G.add_edge(row[0],row[3])
	

print(G.nodes)
print(G.edges)

data_list = []
for ids in G.nodes:
	data_list.append({
		'ids':ids,
		'deg':G.degree[ids]
	})
print(data_list)

with open('csv/deg.csv','w',encoding='utf-8',newline='') as f:
	w = csv.writer(f)
	w.writerow(['id','deg'])
	for d in data_list:
		w.writerow(d.values())